package com.skilltrack.enrol.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.skilltrack.enrol.mapper.CourseTeacherMapper;
import com.skilltrack.enrol.model.po.CourseTeacher;
import com.skilltrack.enrol.service.CourseTeacherService;
import com.skilltrackedu.exception.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;



    /**
     * 查询课程老师列表
     *
     * @param id 课程ID，用于查询与课程关联的老师列表
     * @return 返回一个CourseTeacher对象的列表，表示与指定课程ID关联的老师列表
     */
    @Override
    public List<CourseTeacher> queryCourseTeacherList(Long id) {
        // 创建一个LambdaQueryWrapper对象，用于构建查询条件
        LambdaQueryWrapper<CourseTeacher> qw = new LambdaQueryWrapper<>();
        // 设置查询条件，筛选出课程ID等于参数id的记录
        qw.eq(CourseTeacher::getCourseId, id);
        // 调用Mapper接口的selectList方法，传入查询条件，获取课程老师列表
        return courseTeacherMapper.selectList(qw);
    }



    /**
     * 保存与修改课程老师
     *
     * @param courseTeacher 课程老师
     * @return {@link CourseTeacher}
     */
    @Override
    public CourseTeacher saveCourseTeacher(CourseTeacher courseTeacher) {
        //传id即为修改
        if (courseTeacher.getId() == null) {
            //数据库中设计为课程id与教师姓名为唯一字段，要进行判断不然会报错

            //判断是否有该教师
            CourseTeacher isCourseTeacher = courseTeacherMapper
                    .selectOne(
                            new LambdaQueryWrapper<CourseTeacher>()
                                    .eq(CourseTeacher::getCourseId, courseTeacher.getCourseId())
                                    .eq(CourseTeacher::getTeacherName, courseTeacher.getTeacherName())
                    );

            //如果查到了，则提示已有该教师
            if(isCourseTeacher!=null){
                CommonException.cast("已有该教师!");
            }

            //如果没查到,插入教师信息
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert <= 0) {
                CommonException.cast("保存教师信息失败");
            }
            //在新增操作完成后，MyBatis-Plus 会将生成的主键值自动填充到 courseTeacher 对象中。
            //所以下面的根据id查询才能正常查到
            //返回插入后的教师信息
            return courseTeacherMapper.selectById(courseTeacher);
        } else {
            //更新教师信息
            int i = courseTeacherMapper.updateById(courseTeacher);
            if (i <= 0) {
                CommonException.cast("更新教师失败");
            }
            return courseTeacherMapper.selectById(courseTeacher);
        }
    }



    /**
     * 删除课程老师
     *
     * @param courseId  课程id
     * @param teacherId 老师id
     */
    @Override
    public void deleteCourseTeacher(Long courseId, Long teacherId) {
        // 创建查询条件对象
        LambdaQueryWrapper<CourseTeacher> qw = new LambdaQueryWrapper<>();
        // 设置查询条件：根据课程id和老师id查询
        qw.eq(CourseTeacher::getCourseId, courseId)
                .eq(CourseTeacher::getId,teacherId);
        // 执行删除操作
        int delete = courseTeacherMapper.delete(qw);
        // 如果删除失败，抛出异常
        if (delete<=0) {
            CommonException.cast("删除教师失败");
        }
    }

}